package com.hpin.mlxteacher.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseFragment;
import com.hpin.mlxteacher.my.AboutActivity;
import com.hpin.mlxteacher.my.AddressBookActivity;
import com.hpin.mlxteacher.my.SettingActivity;
import com.hpin.mlxteacher.widget.CircleImageView;

/**
 * 我的fragment
 * Created by Aaron on 2015/7/15.
 */
public class MyFragement extends BaseFragment implements View.OnClickListener {

    private CircleImageView cv_headpic;
    private Intent mIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View myView = inflater.inflate(R.layout.fragment_my, null);

        initWidget(myView);

        return myView;
    }

    private void initWidget(View myView) {

        TextView tv_title = (TextView) myView.findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.rl_my));

        cv_headpic = (CircleImageView) myView.findViewById(R.id.cv_headpic);//头像

        TextView tv_name = (TextView) myView.findViewById(R.id.tv_name);//名称
        TextView tv_version_name = (TextView) myView.findViewById(R.id.tv_version_code);//版本号
        TextView tv_quit = (TextView) myView.findViewById(R.id.tv_quit);//退出

        RelativeLayout rl_personal_setting = (RelativeLayout) myView.findViewById(R.id.rl_personal_setting);//个人设置
        RelativeLayout rl_address_book = (RelativeLayout) myView.findViewById(R.id.rl_address_book);//通讯录
        RelativeLayout rl_about = (RelativeLayout) myView.findViewById(R.id.rl_about);//关于
        RelativeLayout rl_version_code = (RelativeLayout) myView.findViewById(R.id.rl_version_code);//版本号


        rl_personal_setting.setOnClickListener(this);
        rl_address_book.setOnClickListener(this);
        rl_about.setOnClickListener(this);
        rl_version_code.setOnClickListener(this);
        tv_quit.setOnClickListener(this);

        //版本号
        String versionName = getPackageInfo(getActivity()).versionName;
        tv_version_name.setText(versionName);

    }


    public PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }



    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.rl_personal_setting://个人设置

                mIntent = new Intent(getActivity(), SettingActivity.class);
                startActivity(mIntent);

                break;
            case R.id.rl_address_book://通讯录

                mIntent = new Intent(getActivity(), AddressBookActivity.class);
                startActivity(mIntent);
                break;
            case R.id.rl_about://关于

                mIntent = new Intent(getActivity(), AboutActivity.class);
                startActivity(mIntent);
                break;
            case R.id.rl_version_code://版本号


                break;
            case R.id.tv_quit://退出

                quit();

                break;

        }

    }

    //退出
    private void quit() {

        new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage("确定要退出吗?").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                System.exit(0);
            }
        }).show();

    }
}
